package com.zzy.database.controller;

import com.zzy.database.pojo.Customer;
import com.zzy.database.pojo.Employee;
import com.zzy.database.pojo.Result;
import com.zzy.database.service.EmployeeService;
import com.zzy.database.utils.Jwtutil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
import java.util.UUID;


@RestController
@RequestMapping("/employees")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @PostMapping
    public Result addEmployee(@RequestBody Employee employee) {
        if (employee.getPhone() != null && employee.getPhone().length() > 15) {
            return Result.error("电话号码长度不能超过15位");
        }
        try {
            employee.setEmployee_id(UUID.randomUUID().toString());
            employeeService.addEmployee(employee);
            return Result.success("员工添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    @DeleteMapping
    public Result deleteEmployee(@RequestBody Employee employee) {
        try {
            employeeService.deleteEmployee(employee.getEmployee_id());
            return Result.success("员工删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    @PutMapping
    public Result updateEmployee(@RequestBody Employee employee) {
        try {
            employeeService.updateEmployee(employee);
            return Result.success("员工信息更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    @PutMapping("/assign")
    public Result assignEmployeeToBranch(@RequestBody Map<String, String> params) {
        try {
            employeeService.assignEmployeeToBranch(params.get("employee_id"), params.get("branch_id"));
            return Result.success("员工分配成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/getByBranchId")
    public Result getEmployeesByBranchId(@RequestParam(required = false) String branch_id) {
        System.out.println("请求参数: branch_id = " + branch_id); // 添加日志输出
        try {
            List<Employee> employeeList = employeeService.getEmployeesByBranchId(branch_id);
            System.out.println("查询结果: " + employeeList); // 添加日志输出
            return Result.success(employeeList);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    @PostMapping("/completeEmployee")
    public Result complteEmployee(@RequestBody Employee employee, @RequestHeader("Authorization") String authorization){

        Map<String, Object> map = Jwtutil.parseToken(authorization);
        String username = (String) map.get("username");
        String userType=(String) map.get("userType");
        String userid=(String)map.get("id");
        employee.setEmployee_name(username);
        employee.setEmployee_id(userid);
        employeeService.complete(employee);
        return Result.success();
    }
}


